﻿using EntityTest;
using IRepository.Core;
using Project.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EntityFrameworks.Data.Core;
namespace WebTest.Controllers
{
    public class DomainEventController : Controller
    {
        //
        // GET: /DomainEvent/
        public ActionResult Index()
        {

            return View();
        }
        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            EventBus.Instance.Subscribe(new AddUserEventHandler_Log());
            EventBus.Instance.Subscribe(new AddUserEventHandler_Message());
            EventBus.Instance.Subscribe<AddUserEvent>((evt) =>
            {
                evt.HttpContext.Response.Write(string.Format("<span style='color:red'>建立了一个用户,用户ID:{0}</span>", evt.UserID));
            });

            AddUser(new WebManageUsers
            {
                LoginName = form["username"],
                Password = "Password",
                RealName = "RealName",
                Mobile = "Mobile",
                Email = "Email      ",
                Description = "Description",
                DepartmentID = 1,
                CreateDate = DateTime.Now,
                UpdateDate = DateTime.Now,
                Operator = "Operator",
                Status = 100,
                WebSystemID = 0,
            });
            ViewBag.msg = "事件处理成功";
            return View();
        }

        public void AddUser(EntityTest.WebManageUsers entity)
        {
            IUnitOfWork unitOfWork = new backgroundEntities();
            IRepository<WebManageUsers> repository = new DbContextRepository<WebManageUsers>(unitOfWork);
            repository.Insert(entity);
            Project.Events.EventBus.Instance.Publish(new AddUserEvent
            {
                HttpContext = System.Web.HttpContext.Current,
                UnitOfWork = unitOfWork,
                UserID = entity.ManageUserID
            });
        }
    }

    /// <summary>
    /// 添加用户时的事件
    /// </summary>
    [Serializable]
    public class AddUserEvent : EventBase
    {
        public int UserID { get; set; }
        /// <summary>
        /// 当前数据上下文
        /// </summary>
        public IUnitOfWork UnitOfWork { get; set; }
        /// <summary>
        /// 当前HTTP上下文
        /// </summary>
        public HttpContext HttpContext { get; set; }
    }

    /// <summary>
    /// 添加用户时的事件处理程序->日志处理程序
    /// </summary>
    public class AddUserEventHandler_Log : IEventHandler<AddUserEvent>
    {
        public void Handle(AddUserEvent evt)
        {
            evt.HttpContext.Response.SetCookie(new HttpCookie("log", string.Format("添加用户{0},数据上下文{1}", evt.UserID, evt.UnitOfWork.ToString())));
        }
    }
    /// <summary>
    /// 添加用户时的事件处理程序->发消息处理程序
    /// </summary>
    public class AddUserEventHandler_Message : IEventHandler<AddUserEvent>
    {
        public void Handle(AddUserEvent evt)
        {
            evt.HttpContext.Response.SetCookie(new HttpCookie("message", string.Format("给用户{0}发消息,消息内容:您已经建立一个用户了" + DateTime.Now, evt.UserID)));
        }
    }
}
